# Contributor: Konstantin Kulikov <k.kulikov2@gmail.com>
# Maintainer: Konstantin Kulikov <k.kulikov2@gmail.com>

# Keep in sync with grafana-frontend.

pkgname=grafana
pkgver=10.2.3
pkgrel=0
_commit=1e84fede543 # git rev-parse --short HEAD
_stamp=1702908181 # git --no-pager show -s --format=%ct
pkgdesc="Open source, feature rich metrics dashboard and graph editor"
url="https://grafana.com"
# x86, armhf, armv7: many test failures, for example https://github.com/grafana/grafana/issues/26389
# s390x: test failures https://github.com/grafana/grafana/issues/26390
arch="all !s390x !x86 !armhf !armv7"
license="AGPL-3.0-only"
depends="grafana-frontend~=$pkgver"
makedepends="go sqlite-dev wire-go"
checkdepends="mailcap tzdata"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc"
options="net"
source="$pkgname-$pkgver.tar.gz::https://github.com/grafana/grafana/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname-cli.sh
	$pkgname-server.sh"

export GOFLAGS="$GOFLAGS -tags=libsqlite3"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

# secfixes:
#   9.1.2-r0:
#     - CVE-2022-31176
#   9.0.3-r0:
#     - CVE-2022-31097
#     - CVE-2022-31107
#   8.5.3-r0:
#     - CVE-2022-29170
#   8.3.6-r0:
#     - CVE-2022-21702
#     - CVE-2022-21703
#     - CVE-2022-21713
#   8.3.4-r0:
#     - CVE-2022-21673
#   8.3.2-r0:
#     - CVE-2021-43813
#     - CVE-2021-43815
#   8.3.1-r0:
#     - CVE-2021-43798
#   8.2.4-r0:
#     - CVE-2021-41244
#   7.4.5-r0:
#     - CVE-2021-28146
#     - CVE-2021-28147
#     - CVE-2021-28148
#     - CVE-2021-27962
#   7.0.2-r0:
#     - CVE-2020-13379
#   6.3.4-r0:
#     - CVE-2019-15043

build() {
	wire gen -tags oss ./pkg/server ./pkg/cmd/grafana-cli/runner

	local ldflags="-X main.version=$pkgver -X main.commit=$_commit -X main.buildstamp=$_stamp"
	go build -o ./ -ldflags "$ldflags" -v ./pkg/cmd/grafana
}

check() {
	# shellcheck disable=2046
	go test -short $(go list ./... | grep -v api/alerting)
}

package() {
	install -Dm755 "$srcdir/$pkgname.initd" "$pkgdir/etc/init.d/$pkgname"
	install -Dm644 "$srcdir/$pkgname.confd" "$pkgdir/etc/conf.d/$pkgname"
	install -Dm755 "$srcdir/$pkgname-server.sh" "$pkgdir/usr/sbin/$pkgname-server"
	install -Dm755 "$srcdir/$pkgname-cli.sh" "$pkgdir/usr/bin/$pkgname-cli"
	install -Dm755 "$builddir/$pkgname" "$pkgdir/usr/bin/$pkgname"
	install -Dm644 "$builddir/conf/sample.ini" "$pkgdir/etc/grafana.ini"
	install -dm755 "$pkgdir/usr/share/grafana"
	cp -r "$builddir/conf" "$pkgdir/usr/share/$pkgname/"
}

sha512sums="
1ce93b53f36a3e2b29169653e411d2685bf1a86673d897b3280b47f71d244a98decb03e387ad2693fb516df87f89b5e1a42e3c87b6ec9af86c80f12cd7b61118  grafana-10.2.3.tar.gz
52793ecc49d0c355e11d3ae2a5c047b36c7b0dc687bd1b9f24a20e96a477ffe44ee70295d005bb202cf5b0294d63400066a236b7969cd4eb54d65e9f88d2a40d  grafana.initd
055299dd0f3a3a42c7787246f5ffcc2571adcf298a9e0b572313764d532104b8a33becd387b9c7bc4361993e7333a4cb1658d354b0a786b36479f9d8c31adab8  grafana.confd
42472cad4b209fd9d63b0fe9a6a37d832a63309cc8cf08266085461dd6d08da9060b7956d44ef1be5c6a8f2fab5dca0803da00a85578a1bbde25e8c26c92f95d  grafana-cli.sh
7a76cb84b8cd493cdd43373c79c24bfa6a96f756881bce97178438f0dcb755dddc01a6ea5b68eb285761aa553ae4ab21c12af012fc251092dd0f1d0ad858ee12  grafana-server.sh
"
